package net.ywd.xp1024search.test;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * 读取网页内容测试类
 *
 * @Author Dreamingodd
 * @Date 2017/04/19
 */
public class ReadWebContent {
    public static void main(String args[]) throws  Exception {
        System.out.println(read2("http://1024x3.net/pw/thread.php?fid=22&page=1"));
        System.out.println(read1("http://1024x3.net/pw/thread.php?fid=22&page=1"));
    }

    public static String read1(String url) throws Exception {
        String line = "";
        BufferedReader in = new BufferedReader(new InputStreamReader(
                new URL(url).openConnection().getInputStream(), "UTF-8"));//GB2312可以根据需要替换成要读取网页的编码
        StringBuffer sb = new StringBuffer();
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }

    public static String read2(String urlStr) throws Exception {
        String line = null;
        URL url = new URL(urlStr);
        HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
        httpcon.addRequestProperty("User-Agent", "Mozilla/4.76");
        BufferedReader in = new BufferedReader(new InputStreamReader(httpcon.getInputStream(), "UTF-8"));
        StringBuffer sb = new StringBuffer();
        while ((line = in.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }
}
